package com.tanhua.controller;

import com.sms.SmsTemplate;
import com.tanhua.domain.UserInfo;
import com.tanhua.interceptor.UserHolder;
import com.tanhua.service.UserInfoService;
import com.tanhua.service.UserService;
import com.tanhua.util.GenerateCode;
import com.tanhua.util.JWTUtil;
import com.tanhua.vo.CodeVo;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private SmsTemplate smsTemplate;

    @Autowired
    private UserService userService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/login")
    public ResponseEntity sendCode(@RequestBody CodeVo codeVo){
        System.out.println(codeVo.getPhone());
        //1 产生随机验证码
//        String code = GenerateCode.getCode();
        String code = "123456";
        //2 放入redis(redis开了没有？redis的坐标？redis在yml中的配置、引入redisTemplate、确认验证码的key)
        stringRedisTemplate.opsForValue().set("LOGIN_CODE_"+codeVo.getPhone(),code);
        //3调用阿里大鱼发送短信(引入阿里云sms启动器，配置yml文件、引入SmsTemlate、调用)
//        smsTemplate.send(codeVo.getPhone(),code);
        //4 返回
        return ResponseEntity.ok(null);
    }

    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody CodeVo codeVo){

        //1 验证验证码是否ok，ok再去调用service
        // 2从redis中取出验证码
        String redisCode = stringRedisTemplate.opsForValue().get("LOGIN_CODE_" + codeVo.getPhone());
        //3判断验证码
        if (StringUtils.isBlank(redisCode)||!redisCode.equals(codeVo.getVerificationCode())){
            throw new RuntimeException("验证码异常");
        }
        //验证通过-删除验证码
        stringRedisTemplate.delete("LOGIN_CODE_"+codeVo.getPhone());

        //4 比较通过调用service
        Map map = userService.loginVerification(codeVo);

        System.out.println(map.get("token"));

        //5 返回
        return ResponseEntity.ok(map);
    }

    /**
     * 首次登陆，保存用户详细信息到userinfo表中
     * @param userInfo
     * @return
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo){
        //一、获取token，解析token，获取用户id--
//        String token = request.getHeader("Authorization");
//        if (StringUtils.isBlank(token)){
//            throw new RuntimeException("非法token");
//        }
//        //解析token
//        Claims claims = JWTUtil.parseToken(token, "tanhua");
//        if (claims==null){
//            throw new RuntimeException("非法token");
//        }
//        Long userId = Long.valueOf(claims.get("userId").toString());


        userInfo.setId(UserHolder.getUserId());
        //二、远程调用，通过dubbo-db保存到userinfo表中
        userInfoService.save(userInfo);

        return ResponseEntity.ok(null);
    }


    /**
     * 1 获取token，解析token，这个头像给当前登录的人
     * 2 上传头像到阿里云
     * 3 拿着阿里云的url去百度云 人脸检测
     * 4 远程调用--更新数据库
     *
     * @param headPhoto
     * @return
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity head(MultipartFile headPhoto) throws IOException {
        //1 获取token，解析token，这个头像给当前登录的人
//        String token = request.getHeader("Authorization");
//        if (StringUtils.isBlank(token)){
//            throw new RuntimeException("非法token");
//        }
//        //解析token
//        Claims claims = JWTUtil.parseToken(token, "tanhua");
//        if (claims==null){
//            throw new RuntimeException("非法token");
//        }
//        Long userId = Long.valueOf(claims.get("userId").toString());

        //调用service
        userInfoService.uploadHead(headPhoto,UserHolder.getUserId());
        return ResponseEntity.ok(null);

    }





}
